package com.scpii.api.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

public class InitializeBean implements ServletContextListener {

	private static final Log logger = LogFactory.getLog(InitializeBean.class);

	private final String scpiiApiLocation = "classpath:scpii-api.properties";

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		org.apache.ibatis.logging.LogFactory.useSlf4jLogging();
		if (logger.isDebugEnabled()) {
			logger.debug("loading properties...");
		}
		ResourceLoader loader = new DefaultResourceLoader();
		Resource resource = loader.getResource(scpiiApiLocation);
		InputStream is = null;
		Properties prop = new Properties();

		try {
			is = resource.getInputStream();
			prop.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}

		Constants.IS_DEBUG = Boolean.valueOf(prop.getProperty("IS_DEBUG",
				"false"));

		Constants.RESOURCE_ROOT_PATH = prop.getProperty("RESOURCE_ROOT_PATH");

		Constants.RESOURCE_ROOT_URL = prop.getProperty("RESOURCE_ROOT_URL");

		Constants.API_ROOT_URL = prop.getProperty("API_ROOT_URL");

		Constants.UNIVERSAL_WEB_ROOT_URL = prop
				.getProperty("UNIVERSAL_WEB_ROOT_URL");

		Constants.UNIVERSAL_CONTENT_URL_TEMPLATE = prop
				.getProperty("UNIVERSAL_CONTENT_URL_TEMPLATE");

		Constants.APP_NODES_INTERFACE_URL_TEMPLATE = Constants.API_ROOT_URL
				+ prop.getProperty("APP_NODES_INTERFACE_URL_TEMPLATE");

		Constants.RESOURCE_INTERFACE_URL_TEMPLATE = Constants.API_ROOT_URL
				+ prop.getProperty("RESOURCE_INTERFACE_URL_TEMPLATE");

		Constants.ARTICLES_INTERFACE_URL_TEMPLATE = Constants.API_ROOT_URL
				+ prop.getProperty("ARTICLES_INTERFACE_URL_TEMPLATE");

		Constants.ADDRESS_INTERFACE_URL_TEMPLATE = Constants.API_ROOT_URL
				+ prop.getProperty("ADDRESS_INTERFACE_URL_TEMPLATE");

		Constants.SMS_SEND_TEMPLATE = prop.getProperty("SMS_SEND_TEMPLATE");

		Constants.INTEGRAL_TIPS_TEMPLATE = prop
				.getProperty("INTEGRAL_TIPS_TEMPLATE");

		Constants.UNIVERSAL_SCHEMA = prop.getProperty("UNIVERSAL_SCHEMA");

		Constants.PUSH_INTERVAL_TIME = prop.getProperty("PUSH_INTERVAL_TIME");

		Constants.PUSH_FTP_HOST = prop.getProperty("PUSH_FTP_HOST");

		Constants.PUSH_FTP_PORT = prop.getProperty("PUSH_FTP_PORT");

		Constants.PUSH_USERNAME = prop.getProperty("PUSH_USERNAME");

		Constants.PUSH_PASSWORD = prop.getProperty("PUSH_PASSWORD");

		Constants.PUSH_DIRECTORY = prop.getProperty("PUSH_DIRECTORY");

		Constants.ALIPAY_NOTIFY_URL = prop.getProperty("ALIPAY_NOTIFY_URL");

		Constants.ALIPAY_CALL_BACK_URL = prop
				.getProperty("ALIPAY_CALL_BACK_URL");

		Constants.ALIPAY_PAY_EXPIRE = prop.getProperty("ALIPAY_PAY_EXPIRE");
		if (is != null) {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (prop != null) {
			prop.clear();
		}
		if (logger.isDebugEnabled()) {
			logger.debug("loaded properties...");
		}
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {

	}

}
